﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp.Domain.Entities;

namespace Zt.QmtqDemo.ConfmPlan
{
    public class ConformPlan : AggregateRoot<Guid>
    {
        protected ConformPlan()
        {
            ExtraProperties = new Dictionary<string, object>();
            ConfmPlanItems = new List<ConformPlanItem>();
        }

        public ConformPlan(
            Guid id,
            string confmPlanNo,
            string orderNo,
            string confmStatus="24",
            string moveInStockNo = "准发库"
            )
        {
            this.Id = id;
            this.ConfmPlanNo = confmPlanNo;
            this.OrderNO = orderNo;
            this.ConfmStatus = confmStatus;
            this.MoveInStockNO = moveInStockNo;
            ExtraProperties = new Dictionary<string, object>();
            ConfmPlanItems = new List<ConformPlanItem>();
        }

        /// <summary>
        /// 准发计划号
        /// </summary>
        public string ConfmPlanNo { get; set; }

        /// <summary>
        /// 默认值是24
        /// </summary>
        public string ConfmStatus { get; set; }

        /// <summary>
        /// 合计材料个数
        /// </summary>
        public int TotalMatNum { get; private set; }

        /// <summary>
        /// 合计材料重量
        /// </summary>
        public decimal TotalMatWt { get; set; }

        /// <summary>
        /// 订单
        /// </summary>
        public string OrderNO { get; set; }

        /// <summary>
        /// 转入库区号
        /// </summary>
        public string MoveInStockNO { get; set; } = "准发库";

        /// <summary>
        /// 计划表明细
        /// </summary>
        public virtual List<ConformPlanItem> ConfmPlanItems { get; set; }

        public virtual void AddConformPlanItem(string conformPlanNo, string matNo, 
            decimal matThick, decimal matWidth, decimal matLen, decimal matwt)
        {
            ConfmPlanItems.AddIfNotContains(new ConformPlanItem(Id, conformPlanNo, matNo, matThick,matWidth,matLen,matwt));
        }

        public virtual void RemoveAllConformPlanItems()
        {
            ConfmPlanItems.Clear();
        }

        public virtual void RemoveConformPlanItem(string conformPlanNo, string matNo)
        {
            ConfmPlanItems.RemoveAll(r =>
                r.ConfmPlanNo == conformPlanNo && r.MatNo == matNo);
        }

        public virtual ConformPlanItem FindConformPlanItem(string conformPlanNo, string matNo)
        {
            return ConfmPlanItems.FirstOrDefault(r =>
                r.ConfmPlanNo == conformPlanNo && r.MatNo == matNo);
        }

        public virtual void SumTotalMatWt()
        {
            this.TotalMatWt = ConfmPlanItems.Sum(x => x.MatWt);
        }

        public virtual void SumTotalMatNum()
        {
            this.TotalMatNum = ConfmPlanItems.Count;
        }     

    }
}
